<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设置 - 桌面宠物</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }

        .settings-container {
            max-width: 360px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }

        .header {
            text-align: center;
            margin-bottom: 25px;
        }

        .header h1 {
            color: #4a5568;
            font-size: 24px;
            margin-bottom: 5px;
        }

        .version {
            color: #718096;
            font-size: 14px;
        }

        .section {
            margin-bottom: 25px;
            padding: 20px;
            background: #f7fafc;
            border-radius: 10px;
            border-left: 4px solid #667eea;
        }

        .section h2 {
            font-size: 18px;
            color: #4a5568;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .btn {
            display: inline-block;
            padding: 12px 24px;
            background: #667eea;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.3s ease;
            text-align: center;
            width: 100%;
            margin-bottom: 10px;
        }

        .btn:hover {
            background: #5a6fd8;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
        }

        .btn:disabled {
            background: #a0aec0;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .btn-success {
            background: #48bb78;
        }

        .btn-success:hover {
            background: #38a169;
        }

        .progress-container {
            margin-top: 15px;
            display: none;
        }

        .progress-bar {
            width: 100%;
            height: 8px;
            background: #e2e8f0;
            border-radius: 4px;
            overflow: hidden;
            margin-bottom: 8px;
        }

        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #667eea, #764ba2);
            width: 0%;
            transition: width 0.3s ease;
        }

        .progress-text {
            font-size: 12px;
            color: #718096;
            text-align: center;
        }

        .status-message {
            padding: 12px;
            border-radius: 8px;
            margin: 15px 0;
            font-size: 14px;
            text-align: center;
            display: none;
        }

        .status-success {
            background: #c6f6d5;
            color: #22543d;
            border: 1px solid #9ae6b4;
        }

        .status-info {
            background: #bee3f8;
            color: #1a365d;
            border: 1px solid #90cdf4;
        }

        .status-warning {
            background: #feebc8;
            color: #744210;
            border: 1px solid #fbd38d;
        }

        .status-error {
            background: #fed7d7;
            color: #742a2a;
            border: 1px solid #feb2b2;
        }

        .update-info {
            background: #edf2f7;
            padding: 15px;
            border-radius: 8px;
            margin: 15px 0;
            display: none;
        }

        .update-info h3 {
            color: #4a5568;
            margin-bottom: 10px;
        }

        .info-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
            font-size: 14px;
        }

        .info-label {
            color: #718096;
        }

        .info-value {
            color: #4a5568;
            font-weight: 500;
        }

        .close-btn {
            background: #e53e3e;
            margin-top: 20px;
        }

        .close-btn:hover {
            background: #c53030;
        }
    </style>
</head>
<body>
<div class="settings-container">
    <div class="header">
        <h1>🐾 桌面宠物设置</h1>
        <div class="version" id="current-version">版本: 加载中...</div>
    </div>

    <div class="section">
        <h2>🔄 资源文件更新</h2>
        <p style="margin-bottom: 15px; color: #718096; font-size: 14px;">
            检查并更新HTML、JS、CSS等资源文件
        </p>
        
        <button class="btn" id="check-update-btn">检查资源更新</button>
        
        <div id="status-message" class="status-message"></div>
        
        <div id="update-info" class="update-info">
            <h3>📦 新资源版本可用</h3>
            <div class="info-row">
                <span class="info-label">当前版本:</span>
                <span class="info-value" id="current-ver">-</span>
            </div>
            <div class="info-row">
                <span class="info-label">最新版本:</span>
                <span class="info-value" id="latest-ver">-</span>
            </div>
            <div class="info-row">
                <span class="info-label">更新内容:</span>
                <span class="info-value" id="changelog">-</span>
            </div>
            <button class="btn btn-success" id="download-update-btn">下载资源更新</button>
        </div>
        
        <div id="progress-container" class="progress-container">
            <div class="progress-bar">
                <div class="progress-fill" id="progress-fill"></div>
            </div>
            <div class="progress-text" id="progress-text">下载进度: 0%</div>
            <div class="progress-files" id="progress-files">正在下载: -</div>
        </div>
    </div>

    <div class="section">
        <h2>⚙️ 其他设置</h2>
        <p style="color: #718096; font-size: 14px;">
            更多设置功能开发中...
        </p>
    </div>

    <button class="btn close-btn" id="close-btn">关闭设置</button>
</div>

<script>
    const { ipcRenderer } = require('electron');

    // DOM元素
    const currentVersionElement = document.getElementById('current-version');
    const checkUpdateBtn = document.getElementById('check-update-btn');
    const downloadUpdateBtn = document.getElementById('download-update-btn');
    const closeBtn = document.getElementById('close-btn');
    const statusMessage = document.getElementById('status-message');
    const updateInfo = document.getElementById('update-info');
    const progressContainer = document.getElementById('progress-container');
    const progressFill = document.getElementById('progress-fill');
    const progressText = document.getElementById('progress-text');
    const progressFiles = document.getElementById('progress-files');

    // 显示状态消息
    function showStatus(message, type = 'info') {
        statusMessage.textContent = message;
        statusMessage.className = `status-message status-${type}`;
        statusMessage.style.display = 'block';
    }

    // 隐藏状态消息
    function hideStatus() {
        statusMessage.style.display = 'none';
    }

    // 获取当前版本
    async function getCurrentVersion() {
        try {
            const version = await ipcRenderer.invoke('get-app-version');
            currentVersionElement.textContent = `版本: v${version}`;
        } catch (error) {
            console.error('获取版本失败:', error);
            currentVersionElement.textContent = '版本: 未知';
        }
    }

    // 检查资源更新
    checkUpdateBtn.addEventListener('click', async () => {
        checkUpdateBtn.disabled = true;
        checkUpdateBtn.textContent = '检查中...';
        hideStatus();
        
        try {
            const result = await ipcRenderer.invoke('check-resource-updates');
            if (result.success) {
                const updateInfo = result.data;
                
                if (updateInfo.hasUpdate) {
                    showStatus(`发现新资源版本 v${updateInfo.remoteVersion}`, 'success');
                    
                    // 显示更新信息
                    document.getElementById('current-ver').textContent = `v${updateInfo.localVersion}`;
                    document.getElementById('latest-ver').textContent = `v${updateInfo.remoteVersion}`;
                    document.getElementById('changelog').textContent = updateInfo.updateInfo.changelog?.join(', ') || '无更新说明';
                    updateInfo.style.display = 'block';
                } else {
                    showStatus('资源文件已是最新版本!', 'info');
                }
            } else {
                showStatus(`检查更新失败: ${result.error}`, 'error');
            }
        } catch (error) {
            showStatus(`检查更新失败: ${error.message}`, 'error');
        } finally {
            checkUpdateBtn.disabled = false;
            checkUpdateBtn.textContent = '检查资源更新';
        }
    });

    // 下载资源更新
    downloadUpdateBtn.addEventListener('click', async () => {
        downloadUpdateBtn.disabled = true;
        downloadUpdateBtn.textContent = '下载中...';
        progressContainer.style.display = 'block';
        progressFill.style.width = '0%';
        progressText.textContent = '下载进度: 0%';
        progressFiles.textContent = '准备下载...';
        
        try {
            const result = await ipcRenderer.invoke('download-resource-updates');
            if (result.success) {
                showStatus('资源文件更新完成! 请重启应用生效。', 'success');
                
                // 提示重启
                setTimeout(() => {
                    ipcRenderer.invoke('restart-app');
                }, 1000);
            } else {
                showStatus(`下载失败: ${result.error}`, 'error');
                downloadUpdateBtn.disabled = false;
                downloadUpdateBtn.textContent = '下载资源更新';
                progressContainer.style.display = 'none';
            }
        } catch (error) {
            showStatus(`下载失败: ${error.message}`, 'error');
            downloadUpdateBtn.disabled = false;
            downloadUpdateBtn.textContent = '下载资源更新';
            progressContainer.style.display = 'none';
        }
    });

    // 关闭窗口
    closeBtn.addEventListener('click', () => {
        window.close();
    });

    // 监听下载进度
    ipcRenderer.on('download-progress', (event, progress) => {
        progressFill.style.width = `${progress.progress}%`;
        progressText.textContent = `下载进度: ${progress.progress}%`;
        document.getElementById('progress-files').textContent = 
            `正在下载: ${progress.filename} (${progress.completed}/${progress.total})`;
    });

    // 初始化
    getCurrentVersion();
</script>
</body>
</html>